package com.homegoods.videomodule.ui.fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.homegoods.resource.dialog.BaseListBottomSheetDialog;
import com.homegoods.videomodule.R;
import com.homegoods.videomodule.model.CommentModel;
import com.homegoods.videomodule.model.FreedomCommunityModel;
import com.homegoods.videomodule.presenter.CommentPresenter;
import com.homegoods.videomodule.ui.activity.SendCommentActivity;
import com.homegoods.videomodule.ui.view.OnPublishCommentListener;
import com.kedll.library.adapter.MultiTypeAdapter;

/**
 * @author QiQi
 * @ClassName: CommentListDialogFragment
 * @date 2018/10/15
 * @description 评论列表
 */
public class CommentListDialogFragment extends BaseListBottomSheetDialog<CommentPresenter> implements OnPublishCommentListener {

    private TextView mComment;
    private int mCommentCount = 56;
    public FreedomCommunityModel model;

    protected int getStyle() {
        return R.style.BottomSheetDialogStyle2;
    }

    public void setData(FreedomCommunityModel model) {
        this.model = model;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
    }

    @Override
    protected int layoutResId() {
        return R.layout.video_fragment_comment;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mComment = view.findViewById(R.id.tv_comment);
        mComment.setOnClickListener(v -> startActivityForResult(new Intent(getContext(), SendCommentActivity.class), 100));

//
//        adapter.setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<CommentModel>) (v, iItem) -> {
//            AllCommentListDialogFragment allCommentListDialogFragment = new AllCommentListDialogFragment();
//            Bundle bundle = new Bundle();
//            bundle.putParcelable("data", iItem.getData());
//            allCommentListDialogFragment.setArguments(bundle);
//            allCommentListDialogFragment.showDialog(getFragmentManager(), "allComment");
//        });
    }

    @Override
    protected void loadData(Bundle arguments) {
        setCommentCount(mCommentCount);
        super.loadData(arguments);
    }

    @Override
    public CommentPresenter getPresenter() {
        return new CommentPresenter(this, model.getSid());
    }

    private void setCommentCount(int commentCount) {
        setTitle(String.format("%s条评论", commentCount));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK || data == null) {
            return;
        }

        if (requestCode == 100) {
            String comment = data.getStringExtra("result");
            publishComment(comment);
            setCommentCount(++mCommentCount);
        }
    }

    @Override
    public void publishComment(String comment) {
        mList.smoothScrollToPosition(0);
        mPresenter.publishComment(comment);
    }

}
